/**
 * Javascript Data Formatter component (ZJB) v1.0
 * require:
 *			jQuery >= 1.11.1
 */
(function ($) {

	if (typeof $.zjb === 'undefined')
		$.zjb = {};

	$.zjb.formatters = {};

	/********** 静态函数 **********/

	$.zjb.formatters.dateFormatter = function (value, locally) {
		if (!value)
			return '';

		var idx = !locally ? 0 : 1;
		var units = [
			['-',  '-',  ''],
			['年', '月', '日']
		];
		var unit = units[idx];

		var toFixedLength = $.zjb.formatters.fixedLengthIntegerFormatter;
		try {
			var a = new Date($.zjb.dateKit.safeDateString(value));
			return (a.getYear() + 1900) + unit[0] +
				toFixedLength(a.getMonth() + 1, 2) + unit[1] +
				toFixedLength(a.getDate(), 2) + unit[2];
		} catch (e) {
			return '';
		}
	};
	
	$.zjb.formatters.timeFormatter = function (value) {
		if (!value)
			return '';

		var toFixedLength = $.zjb.formatters.fixedLengthIntegerFormatter;
		try {
			var a = new Date($.zjb.dateKit.safeDateString(value));
			return '' +
				toFixedLength(a.getHours(), 2) + ':' + 
				toFixedLength(a.getMinutes(), 2) + ':' +
				toFixedLength(a.getSeconds(), 2);
		} catch (e) {
			return '';
		}
	};
	
	$.zjb.formatters.hourMinuteFormatter = function (value) {
		if (!value)
			return '';

		var toFixedLength = $.zjb.formatters.fixedLengthIntegerFormatter;
		try {
			var a = new Date($.zjb.dateKit.safeDateString(value));
			return '' +
				toFixedLength(a.getHours(), 2) + ':' + 
				toFixedLength(a.getMinutes(), 2);
		} catch (e) {
			return '';
		}
	};

	$.zjb.formatters.datetimeFormatter = function (value, locally) {
		if (!value)
			return '';

		var idx = !locally ? 0 : 1;
		var units = [
			['-',  '-',  ''],
			['年', '月', '日']
		];
		var unit = units[idx];

		var toFixedLength = $.zjb.formatters.fixedLengthIntegerFormatter;
		try {
			var a = new Date($.zjb.dateKit.safeDateString(value));
			return (a.getYear() + 1900) + unit[0] +
				toFixedLength(a.getMonth() + 1, 2) + unit[1] +
				toFixedLength(a.getDate(), 2) + unit[2] + ' ' +
				toFixedLength(a.getHours(), 2) + ':' +
				toFixedLength(a.getMinutes(), 2) + ':' +
				toFixedLength(a.getSeconds(), 2);
		} catch (e) {
			return '';
		}
	};

	$.zjb.formatters.fixedLengthTextFormatter = function (text, len, chr) {
		var pld = chr || '0';
		var str = text.toString();
		while (str.length < len)
			str = pld + str;
		return str;
	};

	$.zjb.formatters.fixedPointFloatFormatter = function (value, precision) {
		var p = precision >= 0 ? precision : 0;
		var val = value || 0;
		try {
			if (typeof val == 'string' && /^\d+(\.\d+)$/.test(val))
				val = parseFloat(val);
			val = val.toFixed(precision);
		} catch (e) {
			val = 0;
			val = val.toFixed(precision);
		}
		return val;
	};

	$.zjb.formatters.fixedLengthIntegerFormatter = function (value, len, chr) {
		if (!value)
			value = 0;

		var pld = chr || '0';
		var str = value.toString();
		while (str.length<len)
			str = pld + str;
		return str;
	};

	$.zjb.formatters.boolFormatter = function (value, defaultValue) {
		if (!!value)
			return '是';
		
		if (!defaultValue && defaultValue != '')
			return '否';
		
		return defaultValue;
	};

	$.zjb.formatters.moneyFormatter = function (value) {
		if (!value)
			return '免费';

		var v = $.zjb.formatters.fixedPointFloatFormatter(value, 2);
		return v + ' 元';
	};

	$.zjb.formatters.genderMaleFormatter = function (value) {
		return !!value ? '男' : '女';
	};

	$.zjb.formatters.successFormatter = function (value) {
		return !!value ? '成功' : '失败';
	};

	$.zjb.formatters.domFormatter = function (value) {
		if (typeof value !== 'string')
			return value;
		
		var dom = null;
		try {
			dom = $(value);
		} catch (e) {
			return value;
		}
		
		if (dom.length == 0)
			return value;
		
		return dom;
	};

})(jQuery);